package routes

import (
	"MulticloudManagement/internal/handler"
	"MulticloudManagement/internal/middleware"
	"MulticloudManagement/pkg/utils"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"path/filepath"
)

// SetupRoutes 启动路由
func SetupRoutes(db *gorm.DB, config *utils.Config) *gin.Engine {
	// 创建路由引擎
	r := gin.Default()
	// 配置模板引擎
	templatePath := filepath.Join(utils.GetProjectPath(), "/templates/*")
	r.LoadHTMLGlob(templatePath)
	// 创建Auth对象
	auth := handler.NewAuth(db, config)

	// 添加路由
	r.GET("/login", auth.LoginView)
	r.POST("/login", auth.Login)
	r.GET("/register", auth.RegisterView)
	r.POST("/register", auth.Register)
	r.GET("/logout", auth.Logout)

	// 放入到一个路由组中,使用一个路由组中间件,就可以验证token
	admin := r.Group("/admin", middleware.JWTAuth())
	{
		admin.GET("/hostlist", auth.HostList)
		admin.POST("/createHost", auth.CreateList)
		admin.DELETE("/deletehost", auth.DeleteList)
	}

	// 返回路由引擎
	return r
}
